package com.zy.experience_system.service.impl;

import com.zy.experience_system.dto.TestAndSetDTO;
import com.zy.experience_system.dto.TestInformationDTO;
import com.zy.experience_system.entity.*;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.SetAndTestForm;
import com.zy.experience_system.form.TestForm;
import com.zy.experience_system.form.TestSimpleForm;
import com.zy.experience_system.repository.*;
import com.zy.experience_system.service.TestService;
import com.zy.experience_system.util.MyDateUtil;
import com.zy.experience_system.vo.TestVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private TestInformationRepository testInformationRepository;
    @Autowired
    private TestAndSetRepository testAndSetRepository;
    @Autowired
    private StuHomeworkRepository stuHomeworkRepository;
    @Autowired
    private StuAndSetRepository stuAndSetRepository;
    @Autowired
    private TestSetRepository testSetRepository;
    @Autowired
    private StuInformationRepository stuInformationRepository;
    @Autowired
    private TeachInformationRepository teachInformationRepository;

    @Override
    public List<TestVO> findAll() {
        List<TestInformation> testInformationList = testInformationRepository.findAll();
        List<TestVO> testVOList = new ArrayList<>();
        for (TestInformation testInformation : testInformationList) {
            TestVO testVO = new TestVO();
            BeanUtils.copyProperties(testInformation, testVO);
            String createTime = MyDateUtil.date2String(testInformation.getCreateTime());
            String updateTime = MyDateUtil.date2String(testInformation.getUpdateTime());
            testVO.setCreateTime(createTime);
            testVO.setUpdateTime(updateTime);
            testVOList.add(testVO);
        }
        return testVOList;
    }

    @Override
    public TestVO findByTestId(Integer testId) {
        if (testId != null) {
            boolean b = testInformationRepository.findById(testId).isPresent();
            TestInformation testInformation = testInformationRepository.findById(testId).get();
            if (b) {
                TestVO testVO = new TestVO();
                BeanUtils.copyProperties(testInformation, testVO);
                String createTime = MyDateUtil.date2String(testInformation.getCreateTime());
                String updateTime = MyDateUtil.date2String(testInformation.getUpdateTime());
                testVO.setCreateTime(createTime);
                testVO.setUpdateTime(updateTime);
                return testVO;
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.FAIL);

    }

    @Override
    public Integer save(TestForm testForm) {
        if (testForm != null) {
            TestInformation testInformation;
            if (testForm.getId() == null) {
                testInformation = new TestInformation();
            } else {
                testInformation = testInformationRepository.findById(testForm.getId()).get();
            }
            BeanUtils.copyProperties(testForm, testInformation);
            String author = testForm.getAuthor();
            String name = testForm.getName();
            String type = testForm.getType();
            String teacher = testForm.getTeacher();
            if (author != null && name != null && type != null && teacher != null) {
                testInformation.setAuthorName(author);
                testInformation.setTestName(name);
                testInformation.setTeachId(teacher);
                testInformation.setTestType(type);
                TestInformation testInformation1 = testInformationRepository.save(testInformation);
                return testInformation1.getTestId();
            }
            throw new CommonException(ResultEnum.FAIL);
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public List<TestVO> findAllByTeachId(String teachId) {
        List<TestInformation> testInformationList = testInformationRepository.findAllByTeachId(teachId);
        if (testInformationList != null) {
            List<TestVO> testVOList = new ArrayList<>();
            for (TestInformation testInformation : testInformationList) {
                TestVO testVO = new TestVO();
                BeanUtils.copyProperties(testInformation, testVO);
                testVOList.add(testVO);
                return testVOList;
            }
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public void delete(Integer testId) {
        boolean b = testInformationRepository.findById(testId).isPresent();
        if (b) {
            testInformationRepository.deleteByTestId(testId);
            return;
        }
        throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public Integer save(TestInformationDTO testInformationDTO) {
        Integer testId = testInformationDTO.getTestId();
        TestInformation testInformation;
        if (testId != null) {
            testInformation = testInformationRepository.findById(testId).get();
        } else {
            testInformation = new TestInformation();
        }
        BeanUtils.copyProperties(testInformationDTO, testInformation);
        TestInformation testInformation1 = testInformationRepository.save(testInformation);
        return testInformation1.getTestId();
    }


    @Override
    public void save(SetAndTestForm setAndTestForm) {
        Integer setId = setAndTestForm.getSet();
        List<TestSimpleForm> testSimpleFormList = setAndTestForm.getTest();
        List<TestAndSetDTO> list = testSimpleFormList.stream()
                .map(e -> new TestAndSetDTO(
                        e.getId(),
                        setId,
                        e.getType()
                )).collect(Collectors.toList());
        for (TestAndSetDTO testAndSetDTO : list) {
            TestAndSet testAndSet = new TestAndSet();
            BeanUtils.copyProperties(testAndSetDTO, testAndSet);
            List<TestAndSet> testAndSets = testAndSetRepository.findAllByTestIdAndSetId(testAndSet.getTestId(), testAndSet.getSetId());
            if (testAndSets.size() == 0) {
                testAndSetRepository.save(testAndSet);
            }
            Integer testId = testAndSet.getTestId();
            List<StuAndSet> stuAndSetList = stuAndSetRepository.findAllBySetIdAndOpen(setId, "yes");
            if (stuAndSetList.size() != 0) {
                for (StuAndSet stuAndSet : stuAndSetList) {
                    String stuId = stuAndSet.getStuId();
                    saveHomework(stuId, testId, setId);
                }
            }
        }
    }

    @Override
    public void saveHomework(String stuId, Integer testId, Integer setId) {
        if (testSetRepository.findById(setId).isPresent()) {
            TestInformation testInformation = testInformationRepository.findById(testId).get();
            String testName = testInformation.getTestName();
            String testType = testInformation.getTestType();
            TestSet testSet = testSetRepository.findById(setId).get();
            String setName = testSet.getSetName();
            String stuName;
            if (stuInformationRepository.findAllByStuId(stuId).size()!=0){
                StuInformation stuInformation = stuInformationRepository.findByStuId(stuId);
                stuName = stuInformation.getStuName();
            } else{
                TeachInformation teachInformation = teachInformationRepository.findByTeachId(stuId);
                stuName = teachInformation.getTeachName();
            }
            StuHomework stuHomework = new StuHomework();
            stuHomework.setStuId(stuId);
            stuHomework.setStuName(stuName);
            stuHomework.setTestId(testId);
            stuHomework.setTestName(testName);
            stuHomework.setTestType(testType);
            stuHomework.setSetId(setId);
            stuHomework.setSetName(setName);
            stuHomeworkRepository.save(stuHomework);
            return;
        }
        throw new CommonException(ResultEnum.FAIL);
    }


    @Override
    public void delete(Integer testId, Integer setId) {
        List<TestAndSet> testAndSets = testAndSetRepository.findAllByTestIdAndSetId(testId, setId);
        if (testAndSets.size() != 0) {
            testAndSetRepository.deleteByTestIdAndSetId(testId, setId);
            stuHomeworkRepository.deleteAllByTestIdAndSetId(testId,setId);
            return;
        }
        throw new CommonException(ResultEnum.FAIL);
    }
}
